package com.boilermaker.lotterysystem.dao.mapper;

import com.boilermaker.lotterysystem.dao.dataobject.PrizeDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PrizeMapper {

    @Insert("insert into prize (name, image_url, price, description)" +
            " values (#{name}, #{imageUrl}, #{price}, #{description})")
    // 该注解用于插入数据后立即获取数据库自动生成的主键值，将其映射到 prizeDO 中指定的 keyProperty
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(PrizeDO prizeDO);

    @Select("select count(*) from prize")
    Integer count();

    @Select("select id, name, description, price, image_url from prize" +
            " order by id desc limit #{offset}, #{pageSize}")
    List<PrizeDO> findPrizeListByPage(@Param("offset") Integer offset,
                                      @Param("pageSize") Integer pageSize);

    @Select("<script>" +
            " select id from prize" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            "   #{item}" +
            " </foreach>" +
            "</script>")
    List<Long> selectExistPrizeIds(@Param("items") List<Long> prizeIdList);

    @Select("<script>" +
            " select id, name, description, price, image_url from prize" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            "   #{item}" +
            " </foreach>" +
            "</script>")
    List<PrizeDO> batchSelectByIds(@Param("items") List<Long> prizeIdList);

    @Select("select id, name, description, price, image_url from prize" +
            " where id = #{prizeId}")
    PrizeDO selectById(Long prizeId);
}
